import cv2
from imageio.v3 import imwrite

import secAnalysis as sec


if __name__ == '__main__':
    # 信息熵分析
    cipher_img_root = '../enc_result/cipher.jpg'
    data_root = r'E:\dataset\VOC\VOCdevkit\VOC2007\JPEGImages\001651.jpg'
    R_entropy,G_entropy,B_entropy = sec.entropy(cipher_img_root)

    print('cipher entropy R:{:.4}'.format(R_entropy))
    print('cipher entropy G:{:.4}'.format(G_entropy))
    print('cipher entropy B:{:.4}'.format(B_entropy))

    data_root = r'E:\dataset\VOC\VOCdevkit\VOC2007\JPEGImages\001651.jpg'
    R_entropy,G_entropy,B_entropy = sec.entropy(data_root)
    print('plaintext entropy R:{:.4}'.format(R_entropy))
    print('plaintext entropy G:{:.4}'.format(G_entropy))
    print('plaintext entropy B:{:.4}'.format(B_entropy))

    # 相邻像素相关性分析

    R_Rxy, G_Rxy, B_Rxy = sec.correlation(data_root,3000)
    print(' R    \t{:.4f}    {:.4f}    {:.4f}'.format(R_Rxy[0], R_Rxy[1], R_Rxy[2]))
    print(' G    \t{:.4f}    {:.4f}    {:.4f}'.format(G_Rxy[0], G_Rxy[1], G_Rxy[2]))
    print(' B    \t{:.4f}    {:.4f}    {:.4f}'.format(B_Rxy[0], B_Rxy[1], B_Rxy[2]))

    R_Rxy, G_Rxy, B_Rxy = sec.correlation(cipher_img_root, 3000)
    print(' R    \t{:.4f}    {:.4f}    {:.4f}'.format(R_Rxy[0], R_Rxy[1], R_Rxy[2]))
    print(' G    \t{:.4f}    {:.4f}    {:.4f}'.format(G_Rxy[0], G_Rxy[1], G_Rxy[2]))
    print(' B    \t{:.4f}    {:.4f}    {:.4f}'.format(B_Rxy[0], B_Rxy[1], B_Rxy[2]))

    # key sensitive
    cipher1_root = r'../enc_result/cipher.jpg'
    cipher2_root = r'../enc_result/cipher2.jpg'
    c1c2 = sec.keySensitiveDiff(cipher1_root, cipher2_root)
    print(c1c2)
    cv2.imwrite('c1-c2.jpg',c1c2)

    # npcr and uaci

    R_npcr,G_npcr,B_npcr =  sec.NPCR(cipher1_root, cipher2_root)
    print('R NPCR \t{:.4f}'.format(R_npcr))
    print('G NPCR \t{:.4f}'.format(G_npcr))
    print('B NPCR \t{:.4f}'.format(B_npcr))

    R_uaci,G_uaci,B_uaci = sec.UACI(cipher1_root, cipher2_root)
    print('R UACI \t{:.4f}'.format(R_uaci))
    print('G UACI \t{:.4f}'.format(G_uaci))
    print('B UACI \t{:.4f}'.format(B_uaci))

    cipher_img = cv2.imread(cipher1_root)
    noise_cipher = sec.salt_and_pepper_noise(cipher_img, proportion=0.005)

    cv2.imwrite('../enc_result/noise_cipher.jpg', noise_cipher)